CREATE PROCEDURE Document_FindAll
AS
BEGIN
    Select 
    DocumentId,
    ApplicationId,
    FileName,
    UrlFriendly,
    FileUrl,
    Picture,
    FileType,
    Description,
    Body,
    CreatedById,
    CreteadBy,
    IsActive,
    SortIndex,
    DateCreated,
    DateModified,
    IsDeleted
    from Document
END
GO

CREATE PROCEDURE Document_FindById
@DocumentId int
AS
BEGIN
    Select * from Document where DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_FindByName
@name nvarchar(50)
AS
BEGIN
    Select * from Document where DocumentId = @name
END
GO

CREATE PROCEDURE Document_Insert
        @DocumentId int ,
        @ApplicationId int ,
        @FileName nvarchar (50),
        @UrlFriendly nvarchar (255),
        @FileUrl nvarchar (255),
        @Picture nvarchar (255),
        @FileType nvarchar (50),
        @Description nvarchar (50),
        @Body ntext (1073741823),
        @CreatedById int ,
        @CreteadBy nvarchar (50),
        @IsActive bit ,
        @SortIndex int ,
        @DateCreated datetime ,
        @DateModified datetime ,
        @IsDeleted bit 
AS
BEGIN
INSERT INTO Document
(
        DocumentId,
        ApplicationId,
        FileName,
        UrlFriendly,
        FileUrl,
        Picture,
        FileType,
        Description,
        Body,
        CreatedById,
        CreteadBy,
        IsActive,
        SortIndex,
        DateCreated,
        DateModified,
        IsDeleted
)
VALUES
(
        @DocumentId,
        @ApplicationId,
        @FileName,
        @UrlFriendly,
        @FileUrl,
        @Picture,
        @FileType,
        @Description,
        @Body,
        @CreatedById,
        @CreteadBy,
        @IsActive,
        @SortIndex,
        @DateCreated,
        @DateModified,
        @IsDeleted
)
select @DocumentId = @@IDENTITY
END
GO

CREATE PROCEDURE Document_Update
        @DocumentId int ,
        @ApplicationId int ,
        @FileName nvarchar (50),  
        @UrlFriendly nvarchar (255),  
        @FileUrl nvarchar (255),  
        @Picture nvarchar (255),  
        @FileType nvarchar (50),  
        @Description nvarchar (50),  
        @Body ntext (1073741823),  
        @CreatedById int ,
        @CreteadBy nvarchar (50),  
        @IsActive bit ,
        @SortIndex int ,
        @DateCreated datetime ,
        @DateModified datetime ,
        @IsDeleted bit
AS
BEGIN
UPDATE  Document SET
    ApplicationId = @ApplicationId,
    FileName = @FileName,
    UrlFriendly = @UrlFriendly,
    FileUrl = @FileUrl,
    Picture = @Picture,
    FileType = @FileType,
    Description = @Description,
    Body = @Body,
    CreatedById = @CreatedById,
    CreteadBy = @CreteadBy,
    IsActive = @IsActive,
    SortIndex = @SortIndex,
    DateCreated = @DateCreated,
    DateModified = @DateModified,
    IsDeleted = @IsDeleted
    WHERE DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_Delete
    @DocumentId int
AS
BEGIN
    DELETE FROM Document where DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_Paging
    @PageSize int, 
    @PageNumber int
    AS 
    declare @RowStart int
    declare @RowEnd int
    if @PageNumber > 0 
    BEGIN	
    set @PageNumber = @PageNumber - 1
    set @RowStart = @PageSize * @PageNumber + 1;
    set @RowEnd = @RowStart  + @PageSize - 1;
    SELECT TotalRowCount = COUNT(DocumentId) FROM Document;
    WITH MyTable as 
    (
    SELECT * ,  ROW_NUMBER() OVER (order by SortIndex) as RowNumber From  Document
    )
    SELECT * from MyTable where RowNumber >= @RowStart and RowNumber <= @RowEnd
END
GO

CREATE proc Document_SetActive
@DocumentId int
AS
declare @Type bit
SELECT @Type =  IsActive from Document where DocumentId = @DocumentId
BEGIN
IF(@Type = 1)
Update Document set IsActive = 0 where DocumentId = @DocumentId
else
Update Document set IsActive = 1 where DocumentId = @DocumentId
END
GO

CREATE proc Document_SetSortIndex
@DocumentId int, 
@Type int
AS
declare @CurrentSort int
select @CurrentSort = SortIndex from  Document where DocumentId = @DocumentId
BEGIN
IF(@Type = 1)
Update Document set SortIndex =  @CurrentSort + 1 where DocumentId = @DocumentId
else
Update Document set SortIndex =  @CurrentSort - 1 where DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_RemoveSelectedItem
    @DocumentId int
AS
BEGIN
    UPDATE Document SET IsDeleted = 1 WHERE DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_RestoreSelectedItem
    @DocumentId int
AS
BEGIN
    UPDATE Document SET IsDeleted = 0 WHERE DocumentId = @DocumentId
END
GO

CREATE PROCEDURE Document_RemoveAll
    @DocumentId int
AS
BEGIN
    UPDATE Document SET IsDeleted = 1
END
GO

CREATE PROCEDURE Document_RestoreAll
    @DocumentId int
AS
BEGIN
    UPDATE Document SET IsDeleted = 0
END
GO

